package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.FdsMaterialUsageRulesetMapper;
import com.yunhai.device.service.FdsMaterialUsageRulesetService;
import com.yunhai.model.entity.fds.FdsMaterialUsageRulesetEntity;
import com.yunhai.model.vo.fds.FdsMaterialUsageRulesetQuery;
import com.yunhai.model.vo.fds.FdsMaterialUsageRulesetVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FdsMaterialUsageRulesetServiceImpl implements FdsMaterialUsageRulesetService {

    @Resource
    private FdsMaterialUsageRulesetMapper fdsMaterialUsageRulesetMapper;

    /**
     * 获取 FdsMaterialUsageRuleset 分页列表
     */
    @Transactional
    public IPage getFdsMaterialUsageRulesetPagination(PaginationVo paginationVo, FdsMaterialUsageRulesetQuery fdsMaterialUsageRulesetQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FdsMaterialUsageRulesetEntity> lqs = new LambdaQueryWrapper();
        fdsMaterialUsageRulesetMapper.selectPage(page, lqs);

        List<FdsMaterialUsageRulesetEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FdsMaterialUsageRuleset 所有数据
     *
     */
    @Transactional
    public List<FdsMaterialUsageRulesetEntity> getFdsMaterialUsageRulesetAllList( FdsMaterialUsageRulesetQuery fdsMaterialUsageRulesetQuery ) throws Exception {

        LambdaQueryWrapper<FdsMaterialUsageRulesetEntity> lqs = new LambdaQueryWrapper();
        List<FdsMaterialUsageRulesetEntity> list = fdsMaterialUsageRulesetMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FdsMaterialUsageRuleset 详情
     */
    @Transactional
    public FdsMaterialUsageRulesetEntity getFdsMaterialUsageRulesetDetail( Long id) throws Exception {

        FdsMaterialUsageRulesetEntity fdsMaterialUsageRulesetEntity = fdsMaterialUsageRulesetMapper.selectById(id);
        return fdsMaterialUsageRulesetEntity;
    }


    /**
     * 新增 FdsMaterialUsageRuleset
     * */
    @Transactional
    public void addFdsMaterialUsageRuleset(FdsMaterialUsageRulesetVo fdsMaterialUsageRulesetVo) throws Exception {

        FdsMaterialUsageRulesetEntity fdsMaterialUsageRulesetEntity = new FdsMaterialUsageRulesetEntity();
        ReflectionCopy.copyProperties( fdsMaterialUsageRulesetEntity, fdsMaterialUsageRulesetVo );
        fdsMaterialUsageRulesetMapper.insert(fdsMaterialUsageRulesetEntity);
        return;
    }


    /**
     * 更新 FdsMaterialUsageRuleset
     * */
    @Transactional
    public void updateFdsMaterialUsageRuleset( FdsMaterialUsageRulesetVo fdsMaterialUsageRulesetVo) throws Exception {

        if( fdsMaterialUsageRulesetVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FdsMaterialUsageRulesetEntity fdsMaterialUsageRulesetEntity = new FdsMaterialUsageRulesetEntity();
        ReflectionCopy.copyProperties( fdsMaterialUsageRulesetEntity, fdsMaterialUsageRulesetVo );
        fdsMaterialUsageRulesetMapper.updateById(fdsMaterialUsageRulesetEntity);
        return;
    }

    /**
     * 删除 FdsMaterialUsageRuleset
     * */
    @Transactional
    public void batchRemoveFdsMaterialUsageRuleset( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fdsMaterialUsageRulesetMapper.deleteBatchIds(ids);
    }

}